package com.talent.jiwen.ui.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.talent.jiwen.R;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.model.GradeInfo;
import com.talent.jiwen.my.ChangeInfoActivity;
import com.talent.jiwen.my.PersonInfoActivity;
import com.talent.jiwen.ui.widgets.flowlayout.FlowLayout;
import com.talent.jiwen.ui.widgets.flowlayout.TagAdapter;
import com.talent.jiwen.ui.widgets.flowlayout.TagFlowLayout;
import com.talent.jiwen.util.MyToast;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import rx.Observable;

/**
 * Created by Administrator on 2017/8/25.
 * 选择分组
 */

public class ChooseGradeDialog extends Dialog{

    TagFlowLayout grade_1;
    TagFlowLayout grade_2;
    TagFlowLayout grade_3;

    List <GradeInfo> Grade1List=new ArrayList<>();
    List <GradeInfo> Grade2List=new ArrayList<>();
    List <GradeInfo> Grade3List=new ArrayList<>();

    String[] grade1String=new String[]{"一年级", "二年级", "三年级", "四年级", "五年级", "六年级"};
    String[] grade2String=new String[]{"七年级", "八年级", "九年级"};
    String[] grade3String=new String[]{"高一", "高二", "高三"};


    private Activity mContext;
    private OnTagClickListener listener;
    private boolean save2Server;

    public ChooseGradeDialog(Activity context) {
        super(context, R.style.myDialogTheme);
        this.mContext = context;
    }

    public ChooseGradeDialog(Activity context , OnTagClickListener listener  , boolean save2Server) {
        super(context, R.style.stardialog);
        this.mContext = context;
        this.listener=listener;
        this.save2Server = save2Server;
    }

    protected ChooseGradeDialog(Activity context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_choose_grade, null);
        setContentView(view);
        //设置dialog大小
        Window dialogWindow = getWindow();
//        dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        WindowManager manager = ((Activity) mContext).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.9);
        params.height =LinearLayout.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(params);
        setCanceledOnTouchOutside(true);
        initView();
    }

    private void initData(){
        for(int i=0;i<grade1String.length;i++){
            Grade1List.add(new GradeInfo(i+1,grade1String[i]));
        }

        for(int i=0;i<grade2String.length;i++){
            Grade2List.add(new GradeInfo(i+7,grade2String[i]));
        }

        for(int i=0;i<grade3String.length;i++){
            Grade3List.add(new GradeInfo(i+10,grade3String[i]));
        }

    }

    TagAdapter<GradeInfo> mTag1Adapter;
    TagAdapter<GradeInfo> mTag2Adapter;
    TagAdapter<GradeInfo> mTag3Adapter;
    private void initView() {

        grade_1 = (TagFlowLayout) findViewById(R.id.grade_1);
        grade_2=(TagFlowLayout) findViewById(R.id.grade_2);
        grade_3=(TagFlowLayout) findViewById(R.id.grade_3);

        setAdapter();
    }


    private void setAdapter(){

            mTag1Adapter = new TagAdapter<GradeInfo>(Grade1List) {
                @Override
                public View getView(FlowLayout parent, int position, final GradeInfo gradeInfo) {
                    TextView tv = (TextView) mContext.getLayoutInflater().inflate(R.layout.tv_tag_custom,
                            grade_1, false);
                    tv.setText(gradeInfo.getGradeName());

                    if (gradeInfo.isSelected()){
                        tv.setBackgroundResource(R.drawable.tag_common_blue);
                        tv.setTextColor(ContextCompat.getColor(getContext() , R.color.white));
                    }else{
                        tv.setBackgroundResource(R.drawable.tag_kuang_blue);
                        tv.setTextColor(ContextCompat.getColor(getContext() , R.color.text_gray_9));
                    }

                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            gradeInfo.setSelected(true);
                            for (GradeInfo data1 : Grade1List){
                                if (data1.getGradeId() != gradeInfo.getGradeId()){
                                    data1.setSelected(false);
                                }
                            }
                            mTag1Adapter.notifyDataChanged();

                            for (GradeInfo data2 : Grade2List){
                                data2.setSelected(false);
                            }
                            mTag2Adapter.notifyDataChanged();

                            for (GradeInfo data3 : Grade3List){
                                data3.setSelected(false);
                            }
                            mTag3Adapter.notifyDataChanged();

                            updateStudentGrade(gradeInfo);
//                            listener.onClick(ChooseGradeDialog.this,gradeInfo);
//                            ChooseGradeDialog.this.dismiss();
                        }
                    });
                    return tv;
                }
            };
        grade_1.setAdapter(mTag1Adapter);

        mTag2Adapter = new TagAdapter<GradeInfo>(Grade2List) {
            @Override
            public View getView(FlowLayout parent, int position, final GradeInfo gradeInfo) {
                TextView tv = (TextView) mContext.getLayoutInflater().inflate(R.layout.tv_tag_custom,
                        grade_2, false);
                tv.setText(gradeInfo.getGradeName());

                if (gradeInfo.isSelected()){
                    tv.setBackgroundResource(R.drawable.tag_common_blue);
                    tv.setTextColor(ContextCompat.getColor(getContext() , R.color.white));
                }else{
                    tv.setBackgroundResource(R.drawable.tag_kuang_blue);
                    tv.setTextColor(ContextCompat.getColor(getContext() , R.color.text_gray_9));
                }

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        for (GradeInfo data1 : Grade1List){
                            data1.setSelected(false);
                        }
                        mTag1Adapter.notifyDataChanged();

                        gradeInfo.setSelected(true);
                        for (GradeInfo data2 : Grade2List){
                            if (gradeInfo.getGradeId() != data2.getGradeId()){
                                data2.setSelected(false);
                            }
                        }
                        mTag2Adapter.notifyDataChanged();

                        for (GradeInfo data3 : Grade3List){
                            data3.setSelected(false);
                        }
                        mTag3Adapter.notifyDataChanged();

                        updateStudentGrade(gradeInfo);
//
//                        listener.onClick(ChooseGradeDialog.this,gradeInfo);
//                        ChooseGradeDialog.this.dismiss();
                    }
                });

                return tv;
            }
        };

        grade_2.setAdapter(mTag2Adapter);

        mTag3Adapter = new TagAdapter<GradeInfo>(Grade3List) {
            @Override
            public View getView(FlowLayout parent, int position, final GradeInfo gradeInfo) {
                TextView tv = (TextView) mContext.getLayoutInflater().inflate(R.layout.tv_tag_custom,
                        grade_3, false);
                tv.setText(gradeInfo.getGradeName());

                if (gradeInfo.isSelected()){
                    tv.setBackgroundResource(R.drawable.tag_common_blue);
                    tv.setTextColor(ContextCompat.getColor(getContext() , R.color.white));
                }else{
                    tv.setBackgroundResource(R.drawable.tag_kuang_blue);
                    tv.setTextColor(ContextCompat.getColor(getContext() , R.color.text_gray_9));
                }

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (GradeInfo data1 : Grade1List){
                            data1.setSelected(false);
                        }
                        mTag1Adapter.notifyDataChanged();

                        for (GradeInfo data2 : Grade2List){
                            data2.setSelected(false);
                        }
                        mTag2Adapter.notifyDataChanged();

                        gradeInfo.setSelected(true);
                        for (GradeInfo data3 : Grade3List){
                            if (gradeInfo.getGradeId() != data3.getGradeId()){
                                data3.setSelected(false);
                            }
                        }
                        mTag3Adapter.notifyDataChanged();
                        updateStudentGrade(gradeInfo);
//                        listener.onClick(ChooseGradeDialog.this,gradeInfo);
//                        ChooseGradeDialog.this.dismiss();
                    }
                });

                return tv;
            }
        };
        grade_3.setAdapter(mTag3Adapter);
    }

    public void setTagSelect(int GradeId){

        if(GradeId>=1 && GradeId<=6){//小学
//            Set<Integer> set = new HashSet<>();
//            set.add(GradeId-1);
//            if(mTag1Adapter!=null){
//                mTag1Adapter.setSelectedList(set);
//            }
            Grade1List.get(GradeId - 1).setSelected(true);
            if(mTag1Adapter!=null){
                mTag1Adapter.notifyDataChanged();
            }
        }

        if(GradeId>=7 && GradeId<=9){//中学
//            Set<Integer> set = new HashSet<>();
//            set.add(GradeId-7);
//            if(mTag2Adapter!=null){
//                mTag2Adapter.setSelectedList(set);
//            }
            Grade2List.get(GradeId - 7).setSelected(true);
            if(mTag2Adapter!=null){
                mTag2Adapter.notifyDataChanged();
            }
        }

        if(GradeId>=10 && GradeId<=12){//高中
//            Set<Integer> set = new HashSet<>();
//            set.add(GradeId-10);
//            if(mTag3Adapter!=null){
//                mTag3Adapter.setSelectedList(set);
//            }
            Grade3List.get(GradeId - 10).setSelected(true);
            if(mTag3Adapter!=null){
                mTag3Adapter.notifyDataChanged();
            }
        }

    }


    public interface OnTagClickListener{
        void onClick(Dialog dialog, GradeInfo gradeInfo);
    }


    /**更新学生年级
     *
     * @param gradeInfo
     */
    private void updateStudentGrade(final GradeInfo gradeInfo){
        if (save2Server){
            Map<String, String> map = new HashMap<>();
            map.put("studentId" , SPUtil.getUserId());
            map.put("gradeLevel" , gradeInfo.getGradeId() + "");

            Observable observable = ApiFactory.getEducationApiSingleton().updateStudentInfo(SPUtil.stringOut(SPConstant.TOKEN) , map);
            HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(getContext()) {
                @Override
                protected void _onNext(Void v) {
                    listener.onClick(ChooseGradeDialog.this,gradeInfo);
                    ChooseGradeDialog.this.dismiss();
                }

                @Override
                protected void _onError(String message) {
                    MyToast.show(getContext() , message);
                    ChooseGradeDialog.this.dismiss();
                }
            } ,false);
        }else{
            listener.onClick(ChooseGradeDialog.this,gradeInfo);
            ChooseGradeDialog.this.dismiss();
        }

    }
}
